<?php
    $arr = API::templateSearch(array());
    
    # Poimi templatet
    $templates = array();
    if (!empty($arr["list"])) {
        foreach ($arr["list"] as $template) {
            $templates[$template["hashid"]] = $template["extratext"];
        }
    }
    
    $copy       = arrGet($_POST,"copy",0);
    $savecopy   = arrGet($_POST,"savecopy",0);
    
    $hashid = arrGet($_POST,"hashid");
    if ($hashid) {
        $program = API::trainingProgramLoad(array("hashid" => $hashid));
        if ($copy || $savecopy) {
            if (empty($program)) {                
                $savecopy = 0;
            }
            $hashid = null;
        }
    }
        
    $now = time();
    $plus60days = strtotime("+60 days");
?>

<div role="tabpanel">
    <ul class="nav nav-tabs" id="addtrainingprogram-ul-navtab" role="tablist">
        <li role="presentation" class="active">
            <a href="#addtrainingprogram-div-overview" aria-controls="addtrainingprogram-div-overview" role="tab" data-toggle="tab">
                Training program
            </a>
        </li>
        <li role="presentation">
            <a href="#addtrainingprogram-div-templates" aria-controls="addtrainingprogram-div-templates" role="tab" data-toggle="tab">
                Templates
            </a>
        </li>
    </ul>
    
    <div class="tab-content" id="addtrainingprogram-div-tabcontent">
        <div class="tab-pane active" id="addtrainingprogram-div-overview" role="tabpanel">
            <h4>
                Training program
            </h4>
            <div class="form-group">
                <label for="addtrainingprogram-input-program-name">
                    Program name
                </label>
                <?php
                    FT::input("program_name",null,array(
                        "class"         => "form-control",
                        "id"            => "addtrainingprogram-input-program-name",
                        "maxlength"     => 100,
                        "placeholder"   => "Required"
                    ));
                ?>
            </div>

            <div class="form-group">
                <label for="addtrainingprogram-input-startdate">
                    Start date
                </label>
                <div class="input-group date form_date" data-date-format="yyyy-mm-dd">
                    <?php
                        FT::input("startdate",date("Y-m-d",$now),array(
                            "class"     => "form-control",
                            "id"        => "addtrainingprogram-input-startdate",
                            "maxlength" => 10,
                            "readonly"  => "readonly"
                        ));
                    ?>
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-th"></span>
                    </span>
                </div>
            </div>
            
            <div class="form-group">
                <label for="addtrainingprogram-input-enddate">
                    End date
                </label>
                <div class="input-group date form_date" data-date-format="yyyy-mm-dd">
                    <?php
                        FT::input("enddate",date("Y-m-d",$plus60days),array(
                            "class"     => "form-control",
                            "id"        => "addtrainingprogram-input-enddate",
                            "maxlength" => 10,
                            "readonly"  => "readonly"
                        ));
                    ?>
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-th"></span>
                    </span>
                </div>
            </div>
            
            <div class="form-group">
                <label for="addtrainingprogram-textarea-record">
                    Notes
                </label>
                <?php
                    FT::textarea("records[][record]",null,array(
                        "class" => "form-control",
                        "id"    => "addtrainingprogram-textarea-record",
                        "rows"  => 5
                    ));
                ?>
            </div>
        </div>
        
        <div class="tab-pane" id="addtrainingprogram-div-templates" role="tabpanel">
            <h4>
                Templates
            </h4>
            <table class="table">
                <thead>
                    <tr>
                        <th>
                            Template
                        </th>
                        <th class="width20pc">
                            Days to next workout
                        </th>
                        <th>
                            &nbsp;
                        </th>
                    </tr>
                </thead>
                
                <tbody id="addtrainingprogram-tbody-template-link"></tbody>
            </table>
            
            <?php
                FT::$autoescape = false;
                FT::button("<span class=\"glyphicon glyphicon-plus-sign\" aria-hidden=\"true\"></span>",array(
                    "class"     => "addtrainingprogram-button btn btn-default",
                    "id"        => "addtrainingprogram-button-add-templatelink",
                    "title"     => "Add workout"
                ));
                FT::$autoescape = true;
            ?>
        </div>
    </div>
</div>

<hr />

<?php
    FT::button("Save",array(
        "class"             => "button-submitform addtrainingprogram-button btn btn-default",
        "disabled"          => "disabled",
        "data-classname"    => "TrainingProgram",
        "data-functionname" => "save"
    ));
    
    echo "&nbsp;";
    
    FT::button("Save & copy",array(
        "class"             => "button-submitform addtrainingprogram-button btn btn-default",
        "id"                => "addtrainingprogram-button-save-and-copy",
        "disabled"          => "disabled",
        "data-classname"    => "TrainingProgram",
        "data-functionname" => "save",
        "title"             => "Save current and copy to new a training program"
    ));
    
    if ($hashid && !empty($program)) {
        FT::hidden("hashid",$hashid,array(
            "id" => "addtrainingprogram-input-hashid"
        ));
    }
?>

<script id="addtrainingprogram-plain-savecopy" type="text/plain">
    <?php echo htmlescape($savecopy); ?>
</script>

<?php if (!empty($templates)) { ?>
    <script id="addtrainingprogram-json-templates" type="application/json">
        <?php echo htmlescape(json_encode($templates)); ?>
    </script>
<?php } ?>

<?php if (!empty($program)) { ?>
    <script id="addtrainingprogram-json-training-program" type="application/json">
        <?php echo htmlescape(json_encode($program)); ?>
    </script>
<?php } ?>

<script type="text/javascript">
    $(".form_date").datetimepicker({
        autoclose:      1,
        minView:        2,
        todayBtn:       1,		
        todayHighlight: 1,
        weekStart:      1
    });
    
    $("#addtrainingprogram-input-program-name").focus();
    
    if (getElementText("#addtrainingprogram-plain-savecopy") == 1) {
        setError.call($("#addtrainingprogram-button-save-and-copy"),{
            "code": 0
        },"Training program saved and copied!");
    }
    
    __APICALL_FUNC["TrainingProgram/save"] = function(result) {
        var override = null;
        
        if (result.code == 0) {            
            override = "Training program saved!";
            
            if ($(this).attr("id") == "addtrainingprogram-button-save-and-copy") {
                getView("addtrainingprogram",{
                    "hashid":   result.data,
                    "savecopy": 1
                });
                return;
            } else if (!$("#addtrainingprogram-input-hashid").length) {
                viewAddtrainingprogram.resetForm();
            }
        }
        
        setError.call(this,result,override);
    }
    
    getViewScript("addtrainingprogram",function(){
        if ($("#addtrainingprogram-json-templates").length) {
            viewAddtrainingprogram.templates = getElementJSON("#addtrainingprogram-json-templates");
        }
        
        viewAddtrainingprogram.initializePage();
        
        if ($("#addtrainingprogram-json-training-program").length) {
            viewAddtrainingprogram.loadProgram(getElementJSON("#addtrainingprogram-json-training-program"));
        }
    });
</script>